package com.loubii.account.ui.avtivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Property;
import android.widget.EditText;
import android.widget.Toast;

import com.loubii.account.R;
import com.loubii.account.app.AccountApplication;
import com.loubii.account.bean.UserBean;
import com.loubii.account.constants.UserList;
import com.loubii.account.db.UserBeanDao;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends Activity {

    UserList uE = new UserList();
    final static String TestApp = "text_output";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.login_btn})
    public void loginIndex() {

        // 获取EditText 内容 通过id
        // 用户名
        EditText userText = (EditText)findViewById(R.id.user_val);
        String username = userText.getText().toString();
        // 密码
        EditText passText = (EditText)findViewById(R.id.pass_val);
        String password = passText.getText().toString();
        if(username.equals("")||password.equals("")){
            Toast.makeText(this, "用户或密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        // 用户校验
        if(this.userAuth(username, password)){
            Intent intent;
            intent = new Intent(this, MainTabActivity.class);
            startActivity(intent);
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, "用户或密码不正确！", Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.to_register})
    public void registerUser() {
        Intent intent;
        intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
        String passcode = uE.userEntity.get("123");
        Log.d(TestApp, "UserEntity: " + passcode);
    }

    public boolean userAuth(String username,String password){

        List<UserBean> user_list = AccountApplication.getDaoSession().queryBuilder(UserBean.class)
                .where(UserBeanDao.Properties.Name_id.eq(username))
                .where(UserBeanDao.Properties.Password.eq(password))
                .list();
        if (user_list.size() > 0){
            return true;
        }
        return false;
    }
}
